/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cz.fimuni.pa165.frontend;

import cz.fimuni.pa165.backendapi.dto.EmployeeDTO;
import cz.fimuni.pa165.backendapi.facade.ServiceFacade;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

/**
 *
 * @author Michal
 */
@Controller
public class UserController {

    @Autowired
    private ServiceFacade facade;

    @RequestMapping(value = "/user")
    public String userHome(Model model, HttpServletRequest request) {
        String username = (String) request.getSession().getAttribute("username");
        List<EmployeeDTO> emp = facade.findEmployeeByUsername(username);
        model.addAttribute("employee", emp.get(0));
        return "user/form";
    }

    @RequestMapping(value = "/zmena")
    public String userEdit(@RequestParam(value = "username") String username,
            @RequestParam(value = "newpassword") String newpassword,
            @RequestParam(value = "confirmpassword") String confirmpassword,
            Model model, HttpServletRequest request) {
        boolean valid = true;
        String name = (String) request.getSession().getAttribute("username");
        List<EmployeeDTO> povodny = facade.findEmployeeByUsername(name);
        List<EmployeeDTO> control = facade.findEmployeeByUsername(username);
        if (username.equals("") || (username.trim().length() == 0)) {
            model.addAttribute("userMessage1", "userMessage");
            valid = false;
            return userHome(model, request);
        }
        if (control.size() > 0) {
            if (!control.get(0).getUsername().equals(name)) {
                valid = false;
                model.addAttribute("userMessage1", "userMessage1");
            }
        }
        if (!newpassword.equals(confirmpassword)) {
            model.addAttribute("userMessage2", "userMessage2");
            valid = false;
            return userHome(model, request);
        }
        if ((newpassword.equals("") || newpassword.trim().length() == 0) || (confirmpassword.equals("") || confirmpassword.trim().length() == 0)){
            valid = false;
            model.addAttribute("userMessage3", "userMessage3");
            return userHome(model, request);
        }
        if (valid) {
            EmployeeDTO emp = povodny.get(0);
            emp.setUsername(username);
            emp.setPassword(confirmpassword);
            facade.updateEmployee(emp);
            request.getSession().setAttribute("username", username);
            model.addAttribute("userinfoMessage1", "userinfoMessage1");
        }
        return userHome(model, request);
    }
}
